package cn.lovecyy.mockjson.core.type;

import cn.hutool.core.util.StrUtil;

import cn.lovecyy.mockjson.core.domain.TypeConfig;
import cn.lovecyy.mockjson.core.gen.MockRandomUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * DecimalTypeMocker
 *
 * @author Yakir
 */
public class DecimalTypeMocker implements TypeMocker {

	@Override
	public String getType() {
		return "decimal";
	}

	@Override
	public Object mock(TypeConfig config) {
		String param = config.getParam();
		String format = config.getFormat();
		String mainParam = config.getMainParam();
		BigDecimal val = MockRandomUtils.randomBigDecimalRange(mainParam);
		String retVal;
		if (StrUtil.isNotBlank(format)) {
			// 1. 基础模式：#.##（最多保留两位小数，末尾0不显示）
			DecimalFormat df1 = new DecimalFormat(format);
			retVal = df1.format(val);
		}
		else {
			retVal = val.toPlainString();
		}
		return new BigDecimal(retVal);
	}

}
